﻿#include "mainbalancewidget.h"
#include"ctradebymarketdata.h"

extern CTradeByMarketData* g_pCTradeByMarketData;

MainBalanceWidget::MainBalanceWidget(QWidget *parent) :
    QWidget(parent)
{
    m_dEnableBalance = 0;
    setupModel();
    setupViews();
}

void MainBalanceWidget::setupModel()
{
    m_modelBalance = new QStandardItemModel(0, 11, this);
    m_modelBalance->setHeaderData(0, Qt::Horizontal, QStringLiteral("资金风险度"));
    m_modelBalance->setHeaderData(1, Qt::Horizontal, QStringLiteral("期初权益"));
    m_modelBalance->setHeaderData(2, Qt::Horizontal, QStringLiteral("当前权益"));
    m_modelBalance->setHeaderData(3, Qt::Horizontal, QStringLiteral("持仓盯市盈亏"));
    m_modelBalance->setHeaderData(4, Qt::Horizontal, QStringLiteral("持仓浮动盈亏"));
    m_modelBalance->setHeaderData(5, Qt::Horizontal, QStringLiteral("平仓盯市盈亏"));
    m_modelBalance->setHeaderData(6, Qt::Horizontal, QStringLiteral("平仓浮动盈亏"));
    m_modelBalance->setHeaderData(7, Qt::Horizontal, QStringLiteral("可用资金"));
    m_modelBalance->setHeaderData(8, Qt::Horizontal, QStringLiteral("可取资金"));
    m_modelBalance->setHeaderData(9, Qt::Horizontal, QStringLiteral("手续费"));
    m_modelBalance->setHeaderData(10, Qt::Horizontal, QStringLiteral("持仓保证金"));
}

void MainBalanceWidget::setupViews()
{
    m_hboxlayout = new QHBoxLayout();
    QLabel* accountLabel = new QLabel(QStringLiteral("资金账号"));
    m_accountCombox = new QComboBox;
    m_querybutton = new QPushButton(QStringLiteral("查询"));
    m_exportbutton = new QPushButton(QStringLiteral("导出"));
    accountLabel->setFixedWidth(80);
    m_accountCombox->setFixedWidth(100);
    m_querybutton->setFixedWidth(80);
    m_exportbutton->setFixedWidth(80);

    m_hboxlayout->addWidget(accountLabel);
    m_hboxlayout->addWidget(m_accountCombox);
    m_hboxlayout->addWidget(m_querybutton);
    m_hboxlayout->addWidget(m_exportbutton);
    m_hboxlayout->addStretch();
    m_hboxlayout->addStretch();

    m_MainLayout = new QGridLayout(this);

    m_tableBalance = new QTableView(this);
    m_tableBalance->setAlternatingRowColors(true);
    QFont font = m_tableBalance->horizontalHeader()->font();
    font.setBold(true);
    m_tableBalance->horizontalHeader()->setFont(font);
//    tableOrder->setStyleSheet("QTableView::item:selected { selection-color: rgb(0, 0, 0) }" "QTableView::item:selected { background-color: rgb(255, 255, 0) }"
//                "QTableView{background-color: rgb(0, 0, 0);" "alternate-background-color: rgb(41, 36, 33);}");

    m_tableBalance->setModel(m_modelBalance);
    m_tableBalance->setEditTriggers(QAbstractItemView::NoEditTriggers);
    m_tableBalance->verticalHeader()->setVisible(false); //隐藏列表头
    m_tableBalance->verticalHeader()->setFixedWidth(20);
    m_tableBalance->setSelectionBehavior(QAbstractItemView::SelectRows);
    m_tableBalance->setSelectionMode(QAbstractItemView::SingleSelection);

    m_tableBalance->setColumnWidth(0, 100);
    m_tableBalance->setColumnWidth(1, 100);
    m_tableBalance->setColumnWidth(2, 100);
    m_tableBalance->setColumnWidth(3, 120);
    m_tableBalance->setColumnWidth(4, 100);
    m_tableBalance->setColumnWidth(5, 100);
    m_tableBalance->setColumnWidth(6, 100);
    m_tableBalance->setColumnWidth(7, 100);
    m_tableBalance->setColumnWidth(8, 100);
    m_tableBalance->setColumnWidth(9, 100);
    m_tableBalance->setColumnWidth(10, 100);
    //m_tableBalance->setColumnWidth(11, 100);
    //m_tableBalance->setColumnWidth(12, 100);
    m_MainLayout->addLayout(m_hboxlayout,0,0);
    m_MainLayout->addWidget(m_tableBalance,1,0);

    m_MainLayout->setMargin(0);
    m_MainLayout->setSpacing(0);

	//创建菜单、菜单项
	this->m_tableBalance->setContextMenuPolicy(Qt::CustomContextMenu);
	m_RightPopMenu = new QMenu(this->m_tableBalance);
    m_updateAction = new QAction(QStringLiteral("刷新"),this);
    m_outputAction = new QAction(QStringLiteral("导出"),this);
    m_RightPopMenu->addAction(m_updateAction);
    m_RightPopMenu->addAction(m_outputAction);
    //右键弹出菜单事件绑定
	connect(this->m_tableBalance,SIGNAL(customContextMenuRequested(const QPoint&)),this,SLOT(RightClickedMenuPop(const QPoint&)));
	//查询按钮事件绑定
	connect(m_querybutton,SIGNAL(clicked()),this,SLOT(QueryButtonClicked()));
    //刷新事件绑定
    connect(m_updateAction,SIGNAL(triggered()),this,SLOT(QueryButtonClicked()));
    //导出事件绑定
    connect(m_outputAction,SIGNAL(triggered()),this,SLOT(QueryButtonClicked()));

    m_tableBalance->installEventFilter(this);
}
///右键弹出菜单响应函数
void MainBalanceWidget::RightClickedMenuPop(const QPoint& pos)
{
	m_RightPopMenu->exec(QCursor::pos());
}

bool MainBalanceWidget::eventFilter(QObject *obj, QEvent *event){
    if (event->type() == QEvent::KeyPress) {
        QKeyEvent* keyEvent = (QKeyEvent*)(event);
        emit SigkeyPressEvent(keyEvent);
        return true;
    } else {
        // standard event processing
        return QObject::eventFilter(obj, event);
    }
}

void MainBalanceWidget::QueryButtonClicked(){
    m_modelBalance->removeRows(0,m_modelBalance->rowCount());

    g_pCTradeByMarketData->QryTradingAccount();
}
double MainBalanceWidget::GetEnableBalance(){
    return m_dEnableBalance;
}

void MainBalanceWidget::ShowTradingAccount(IF2UnPacker *lpUnPacker){

	int iIndex = 0;

    //资金风险度
    ((QStandardItemModel*) m_modelBalance)->setItem(iIndex, 0, new QStandardItem(lpUnPacker->GetStr("client_risk_rate")));
    //期初权益
    ((QStandardItemModel*) m_modelBalance)->setItem(iIndex, 1, new QStandardItem(lpUnPacker->GetStr("begin_equity_balance")));
    //当前权益
    ((QStandardItemModel*) m_modelBalance)->setItem(iIndex, 2, new QStandardItem(lpUnPacker->GetStr("equity_balance")));
    //持仓盯市盈亏
    ((QStandardItemModel*) m_modelBalance)->setItem(iIndex, 3, new QStandardItem(lpUnPacker->GetStr("hold_income")));
    //持仓浮动盈亏
    ((QStandardItemModel*) m_modelBalance)->setItem(iIndex, 4, new QStandardItem(lpUnPacker->GetStr("hold_income_float")));
    //平仓盯市盈亏
    ((QStandardItemModel*) m_modelBalance)->setItem(iIndex, 5, new QStandardItem(lpUnPacker->GetStr("drop_income")));
    //平仓浮动盈亏
    ((QStandardItemModel*) m_modelBalance)->setItem(iIndex, 6, new QStandardItem(lpUnPacker->GetStr("drop_income_float")));
    //可用资金
    m_dEnableBalance = lpUnPacker->GetDouble("enable_balance");
    ((QStandardItemModel*) m_modelBalance)->setItem(iIndex, 7, new QStandardItem(lpUnPacker->GetStr("enable_balance")));
    //可取资金
    ((QStandardItemModel*) m_modelBalance)->setItem(iIndex, 8, new QStandardItem(lpUnPacker->GetStr("fetch_balance")));
    //手续费
    ((QStandardItemModel*) m_modelBalance)->setItem(iIndex, 9, new QStandardItem(lpUnPacker->GetStr("business_fare")));
    //持仓保证金
    ((QStandardItemModel*) m_modelBalance)->setItem(iIndex, 10, new QStandardItem(lpUnPacker->GetStr("exch_hold_margin")));

	///静态权益 = 上日结算-出金金额+入金金额  account.PreBalance - account.Withdraw + account.Deposit
	//m_preBalance = pTradingAccount->PreBalance -pTradingAccount->Withdraw + pTradingAccount->Deposit;
	//((QStandardItemModel*) m_modelBalance)->setItem(iIndex, 1, new QStandardItem(QString::number(m_preBalance,'f')));
	///动态权益 = 静态权益+  平仓盈亏+  持仓盈亏-  手续费   静态权益+ account.CloseProfit + account.PositionProfit - account.Commission
	//m_curBalance = m_preBalance + pTradingAccount->CloseProfit + pTradingAccount->PositionProfit - pTradingAccount->Commission;
	//((QStandardItemModel*) m_modelBalance)->setItem(iIndex, 2, new QStandardItem(QString::number(m_curBalance,'f')));
	///可用资金(动态权益-占用保证金-  冻结保证金-  冻结手续费-  交割保证金) account.Available
	///风险度 
	//m_PecentOfRisk =( pTradingAccount->CurrMargin + pTradingAccount->FrozenMargin + pTradingAccount->FrozenCommission)/m_curBalance*100;
	//((QStandardItemModel*) m_modelBalance)->setItem(iIndex, 3, new QStandardItem(QString::number(m_PecentOfRisk,'f')));*/
		
}
